// pages/compass/compass.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    direction: 0,
  },

  onLoad(options) {
    wx.startCompass({
      complete: (res) => {
        this.setData({
          res: res
        })
      },
    })
  },

  onReady() {
    wx.onCompassChange((result) => {

      this.setData({
        res: result,
        direction: result.direction
      })
    })
  },

  /**
   * 打开罗盘
   */
  onStartCompass() {
    wx.startCompass({
      success: (res) => {
        console.log(res)
        this.setData({
          res: res
        })
      },
      fail: (err) => {
        this.setData({
          res: err
        })
      }
    })
  },

  /**
   * 监听罗盘变化事件
   */
  onListenChange() {
    wx.onCompassChange((result) => {
      this.setData({
        res: result
      })
    })
  },

  /**
   * 取消监听罗盘变化事件
   */
  offListenCompass() {
    wx.offCompassChange((result) => {
      this.setData({
        res: result
      })
    })
  },

  /**
   * 关闭罗盘
   */
  onStopCompass() {
    wx.stopCompass({
      success: (res) => {
        console.log(res)
        this.setData({
          res: res
        })
      },
      fail: (err) => {
        this.setData({
          res: err
        })
      }
    })
  }

})